package com.baiyang.app.ui.classification;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.baiyang.app.model.ArticleType;
import com.baiyang.app.tool.LogUtils;
import com.baiyang.app.tool.net.SendMsgUtil;
import com.baiyang.app.tool.net.Urls;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

public class ItemTypeService {

    private Context context;
    private ItemTypeViewModel viewModel;
    public ItemTypeService(Context context,ItemTypeViewModel viewModel){
        this.context = context;
        this.viewModel = viewModel;
    }

    /**
     * 根据数据类型ID获取该类别的图书信息
     * @param typeId
     */
    public void getArticleByTypeId(String typeId,int pageOn){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("typeId",typeId);
        jsonObject.put("pageOn",pageOn);
        jsonObject.put("pageSize",10);
        new SendMsgUtil(){
            @Override
            public void deal(Call call, Response response) throws IOException {
                if(response.code() == 200){
                    String str = response.body().string();
                    LogUtils.i("getArticleByTypeId",str);
                    viewModel.setArticle(JSONObject.parseObject(str));
                }else{
                    LogUtils.i("getArticleByTypeId","code:" + response.code());
                }
            }

            @Override
            public void dealFailure(Call call, IOException e) throws IOException {
                LogUtils.i("getArticleByTypeId","IOException:" + e.getLocalizedMessage());
            }
        }.sendPost(Urls.Article.GET_ARTICLE_BY_TYPE,jsonObject);
    }

}
